<?php
namespace Admin\Model;

use Think\Model\RelationModel;

class WxuserInfoModel extends RelationModel
{
    protected $connection = array(
        'db_type'       =>  'mysql',
        'db_host'       =>  '114.55.30.131',
        'db_port'       =>  '3306',
        'db_name'       =>  'wxsmart',
        'db_user'       =>  'zhkj_crm',
        'db_pwd'        =>  'AdkdCGP7fHcjht5c',
        'db_charset'    =>  'utf8',
        'db_debug'      =>  true,
    );
    protected $tablePrefix = '';
    protected $tableName = 'tp_userinfo';

    protected $_auto = array(
    );

    protected $_link = array(
        'Wxuser'=>array(
            'mapping_type'      => self::BELONGS_TO,
            'mapping_name'        => 'user',
            'foreign_key'        => 'customer_id',
            ),
    );

    public function people_audit($id, $isverify){
        $info = $this->find($id);
        $customerinfo = M('QtwyCustomer', 'tp_', 'DB_WX')->where(array('id' => $info['customer_id']))->find();
        $room_id = $customerinfo['room_id'];

        $verify_time = date("Y-m-d H:i:s",time());
        if($isverify == 1)
        {
            //如果没有审核点了审核，则发送模板消息通知已审核
            $first = "您的业主认证已通过审核!";
            $keyword1 = "通过";
            $remark = "感谢您的使用";


            //青藤小区才调用crm接口
            if ($room_id){
                $data['customer_id'] = $info['customer_id'];
                $data['room_id'] = $room_id;
                // $data['name'] = $info['realname'];
                // $data['tel'] = $info['tel'];
                // $data['sex'] = $info['sex'];
                $url = "http://zhcrm.wx91go.com/index.php?m=Api&c=Data&a=customerAuthentication";
                $ret = apiPost($url,$data);
                $crminfo = json_decode($ret,true);

                $crmid = $crminfo['crm_id'];
                M('QtwyCustomer', 'tp_', 'DB_WX')->where(array('id' => $info['customer_id']))->setField('crm_id',$crmid);
                $crmReturn = ",crmid为$crmid";
            }
        }
        else if($isverify == -1)
        {
            $first = "您的业主认证审核失败!";
            $keyword1 = "不通过";
            $remark = "如有疑问，请联系客服0575-88058110";
        }

        $dataKey    = 'OPENQT201512171';
        $dataArr    = array(
            'wecha_id'		=> $info['wecha_id'],
            'href'			=> "http://smart.wx91go.com/index.php?g=Wap&m=Qt&a=index&token=".$this->token."&wecha_id=".$info['wecha_id'],
            'first'         => $first,
            'keyword1'      => $keyword1,
            'keyword2'      => $verify_time,
            'remark'        => $remark,
        );
        crmSendMsg($dataKey,$dataArr);




        $uinfo['isverify'] = $isverify;
        $uinfo['verify_time'] = time();
        $uinfo['verify_man'] = 'crm:'.session("authId");
        $ok = $this->where(array('id' => $id))->save($uinfo);

        $log['controller'] = __SELF__;
        $log['action'] = ACTION_NAME;
        $log['type'] = "people_audit";
        $log['event'] = "对编号$id 用户进行了审核，审核结果为$isverify ,room_id为$room_id,".$crmReturn;
        $log['rid'] = $id;

        event_log($log);
        if($ok)
            return true;
        else
            return false;
    }

    public function roll(){
        $data = $this->authenticationList(999)['data'];
        $data = array_reverse($data);
        return $data;
    }

    public function authenticationList($pageSize=20){
        $start_time = I('start_time');
        $end_time = I('end_time');
        $community_id = I('community_id');
        $status = I('status');
        $key = I('key');
        $regtime = I('regtime');

        $where = array();
        
        if($regtime) {
            $regtime = strtotime($regtime);
            $where['regtime'] = array('gt',$regtime);
        }

        if($status==1){
            $where['isverify'] = 1;
        }elseif ($status==2){
            $where['isverify'] = 0;
            $where['community_id'] = array('gt',0);
            $where['tel'] = array('gt',0);
        }elseif ($status==3){
            $where['isverify'] = -1;
        }

        if ($start_time)
            $start_time = strtotime($start_time);
        if ($end_time)
            $end_time = strtotime($end_time);
        if (($start_time || $end_time) && $status !=2)
            $where['regtime'] = array('between',"$start_time,$end_time");
        if($community_id)
            $where['community_id'] = array('eq',$community_id);
        if($key){
            if(preg_match("/^\d+$/", $key))
                $where['tel'] = array('like',"%$key%");
            else
                $where['realname'] = array('like',"%$key%");
        }
        $count = $this->where($where)->count();
        $page = new \ZL\Page($count, $pageSize, C('PAGE_01'));
        $data['page'] = $page->show();
        /************************************** 取数据 ******************************************/
        $orderData = $this->relation(true)->where($where)->order('regtime desc')->limit($page->limit.','.$page->listRows)->select();
        foreach ($orderData as $k=>&$v){
            if($v['verify_time'])
                $v['verify_time'] = date('Y-m-d H:i:s',$v['verify_time']);
            if($v['regtime'])
                $v['regtime'] = date('Y-m-d H:i:s',$v['regtime']);
            $v['community_name'] = M('Community')->find($v['community_id'])['name'];
            if ($v['isverify']==-1) {
                $v['status_title'] = '审核失败';
            }elseif ($v['isverify'] ==0 && $v['community_id']!=0){
                $v['status_title'] = '审核中';
            }elseif ($v['isverify'] ==1){
                $v['status_title'] = '审核通过';
            }
        }
        $data['data'] = $orderData;
        return $data;
    }

    public function dumpExcel($pageSize=9999){
        $_data = $this->authenticationList($pageSize)['data'];
        $title = ['小区', '姓名', '手机', '地址', '最后审核时间', '注册时间', '微信名字', '性别', '状态'];
        foreach ($_data as $k=>$v){
            $data[$k][] = $v['community_name'];
            $data[$k][] = $v['user']['name'];
            $data[$k][] = $v['tel'];
            $data[$k][] = $v['address'];
            $data[$k][] = $v['verify_time'];
            $data[$k][] = $v['regtime'];
            $data[$k][] = $v['wechaname'];
            $data[$k][] = ($v['sex']==1?'男':'女');
            $data[$k][] = $v['status_title'];
        }
//        myDump($data);die;
        dumpExcel($title,$data,'认证数据');
    }

    public function _before_write(&$data)
    {
        parent::_before_write($data); // TODO: Change the autogenerated stub
    }

    public function _after_insert($data, $options)
    {
        parent::_after_insert($data, $options); // TODO: Change the autogenerated stub
    }
}
